import os
import cv2
import csv

test_img_dir = './Final_Test/Images/'
test_roi_image_dir = './Final_Test_ROI/'

# Get the csv file
csv_filename = '/GT-final_test.csv'
with open(test_img_dir + csv_filename, 'rb') as f:
    reader = csv.reader(f)
    # Get each image's info
    for row in reader:
        if reader.line_num < 2:
            continue
        img_filename, width, height, x1, y1, x2, y2, label = row[0].split(';')
        # Use OpenCV to read the ppm image files
        img = cv2.imread(test_img_dir + img_filename)
        # Get the ROI
        roi_img = img[int(y1):int(y2), int(x1):int(x2)]
        # Resize the roi image
        roi_img = cv2.resize(roi_img, (64, 64))
        if len(label) < 2:
            label = '0' + label
        # Create the new img filename
        new_filename = label + '_' + img_filename.split('.')[0] + '.jpg'
        # Save the roi img
        cv2.imwrite(test_roi_image_dir + new_filename, roi_img)

print('Test image s roi transforming finished.')
